package com.maniu.openglrecord;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.opengl.EGL14;
import android.opengl.GLES20;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.maniu.openglrecord.utils.OpenGLUtils;

import dalvik.system.DexClassLoader;

public class MainActivity extends AppCompatActivity {
//
private final static int PERMISSION_REQUEST_CODE = 0x01;
    CameraView cameraView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission();

//        你作为程序   java 方法  静态方法   ----》
//        面向过程     基于 gl线程上下文
//        EGL14.eglBindAPI(1);
    }

    public void initView() {
        cameraView = findViewById(R.id.cameraView);
        ((CheckBox)findViewById(R.id.beauty)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                cameraView.enableBeauty(isChecked);
            }
        });
    }

    public void checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA,
                        Manifest.permission.RECORD_AUDIO
                }, PERMISSION_REQUEST_CODE);
            } else {
                initView();
            }
        }else{
            initView();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_REQUEST_CODE) {
            //请求码相同
            if (grantResults.length > 0 &&
                    (grantResults[0] != PackageManager.PERMISSION_GRANTED ||
                            grantResults[1] != PackageManager.PERMISSION_GRANTED ||
                            grantResults[2] != PackageManager.PERMISSION_GRANTED)) {
                //如果结果都存在，但是至少一个没请求成功，弹出提示
                Toast.makeText(this, "请同意必须的应用权限，否则无法正常使用该功能！", Toast.LENGTH_SHORT).show();
            } else if (grantResults.length > 0) {
                //如果结果都存在，两个权限都申请成功，连接服务，启动录屏
                Toast.makeText(this, "权限申请成功，用户同意！", Toast.LENGTH_SHORT).show();
                initView();
            }
        }
    }

    public void startRecord(View view) {
        cameraView.startRecord();
    }

    public void stopRecord(View view) {
        cameraView.stopRecord();
    }
}